#include <bits/stdc++.h>
using namespace std;

const int N = 1e5+5;

typedef long long LL;

int n, m, t, a[N], b[N];

struct Bit
{
    int dat[N];

    void clear(int n)
    {
        memset(dat+1, 0, sizeof(int) * n);
    }

    inline int lowbit(int x)
    {
        return x & (-x);
    }

    inline void add(int i, int x)
    {
        for (; i<N; i+=lowbit(i))
            dat[i] += x;
    }
    inline int sum(int i)
    {
        int res = 0;
        for (; i; i-=lowbit(i))
            res += dat[i];
        return res;
    }

} bit;
int pos[N];
int main(int argc, char const *argv[])
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> t;
    for (int kk=0; kk<t; ++kk)
    {
        cin >> n >> m;

        bit.clear(n);

        for (int i=1; i<=n; ++i) { cin >> a[i]; pos[a[i]] = i; }
        for (int i=1; i<=m; ++i) cin >> b[i];

        int maxdep = 0;
        LL ans = 0;
        for (int i=1; i<=m; ++i)
        {
            bit.add(pos[b[i]], 1);
            if (maxdep >= pos[b[i]]) continue;
            int num = pos[b[i]]-1 - bit.sum(pos[b[i]]-1), price = 2 * num;
            // printf("num=%d\n", num);
            ans += price;
            maxdep = max(maxdep, pos[b[i]]);
        }
        ans += m;

        cout << ans << endl;
    }

    return 0;
}
